import { AxiosError, AxiosResponse, AxiosRequestConfig, Method } from "axios";

export type RequestMethods = Extract<
  Method,
  "get" | "post" | "put" | "delete" | "patch" | "options" | "head"
>;

export interface HttpError extends AxiosError {
  isCancelRequest?: boolean;
}

export interface HttpResponse<T = any> extends Omit<AxiosResponse<T>, 'config'> {
  config: HttpRequestConfig;
}

export interface LoadingHandler {
  start: (code: string) => void;
  end: (code: string) => void;
}

export interface HttpRequestConfig extends AxiosRequestConfig {
  beforeRequestCallback?: (config: HttpRequestConfig) => void;
  beforeResponseCallback?: (response: HttpResponse) => void;
  loadingCode?: string;
  needErrorCode?: boolean;
}

export interface HttpClientConfig {
  baseURL?: string;
  timeout?: number;
  headers?: Record<string, string>;
  // Token获取函数
  getToken?: () => string | null;
  // 加载状态处理器
  loadingHandler?: LoadingHandler;
  // 响应过滤器
  responseFilter?: (response: any) => Promise<any>;
  // 错误处理器
  errorHandler?: (error: HttpError) => any;
  // 全局请求回调
  beforeRequest?: (config: HttpRequestConfig) => void;
  // 全局响应回调
  beforeResponse?: (response: HttpResponse) => void;
}

export interface ApiResponse<T = any> {
  success?: boolean;
  code?: number | string;
  data?: T;
  msg?: string;
  errorMsg?: string;
  errorCode?: string;
}